/**
 * 
 */
package poc.android.weather;

import java.util.ArrayList;
import java.util.List;

import poc.android.weather.model.cuisine.CuisineReviewItem;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Jason
 *
 */
public class ReviewViewListActivity extends ListActivity {

	CuisineReviewApplication application=null;
	ProgressDialog progressDialog=null;
	Thread getReviewThread=null;
	
	Handler getReviewHandler=null;
	List<CuisineReviewItem> reviewList=new ArrayList<CuisineReviewItem>();
	ArrayAdapter<String> reviewListAdapter=null;
	
	ListView listView=null;
	TextView emptyView=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.reviewviewlist);
		
		this.application=(CuisineReviewApplication) this.getApplication();
		/*
		 * create the so-called empty TextView (empty)
		 */
		/*this.emptyView=new TextView(this);
		this.emptyView.setHeight(100);
		this.emptyView.setWidth(100);
		this.emptyView.setText("There is no Data~ based on your selection\r\nlocation: "+
			this.application.getReviewLocation()+"\r\ncuisine: "+
			this.application.getReviewCuisine());*/
		
		this.emptyView=(TextView) this.findViewById(android.R.id.empty);
		
		this.listView=this.getListView();
		this.listView.setItemsCanFocus(false);
		this.listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		this.listView.setEmptyView(this.emptyView);
		
		/**
		 * setup the getReviewHandler thread
		 */
		this.getReviewHandler=new Handler() {

			@Override
			public void handleMessage(Message msg) {
				getProgressDialog().dismiss();
				
				if (reviewList!=null && reviewList.size()>0) {
					reviewListAdapter=new ArrayAdapter<String>(ReviewViewListActivity.this, R.layout.reviewviewlist);
					setListAdapter(reviewListAdapter);
				} else {
					emptyView.setText("No data");
				}
			}
		};
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onResume() {
		super.onResume();
		this.onGetReviews();
	}
	
	private ProgressDialog getProgressDialog() {
		if (this.progressDialog==null) {
			this.progressDialog=new ProgressDialog(this, 3);
			this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			this.progressDialog.setMax(100);
			this.progressDialog.setProgress(0);
			/*this.progressDialog.setSecondaryProgress(this.getProgressDialog().getProgress());*/
		}
		return this.progressDialog;
	}
	
	private void onGetReviews() {
		this.getProgressDialog().setMessage("Processing ...");
		this.getProgressDialog().show();
		
		this.getReviewThread=new Thread() {
			/**
			 * main method for the Thread's logics
			 */
			public void run() {
				// pretend getting Cuisine Object(s) from Web etc
				CuisineReviewItem aItem=new CuisineReviewItem();
				
				aItem.cuisineDescription="Chinese dishes -- special features this week, the GZ meal";
				aItem.cuisineStyle="Chi GuangZhou";
				//reviewList.add(aItem);
				
				aItem.cuisineDescription="UK dishes -- special features this week, the UK meal";
				aItem.cuisineStyle="England UK";
				//reviewList.add(aItem);

				getReviewHandler.sendEmptyMessage(0);				
			}
		};
		this.getReviewThread.start();
		
	}
	
}
